﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using CashFlow.Db.Entity;
using CashFlow.Db.DAL;
using CashFlow.Web.Areas.ShopManage.Models.Fee.Result;

namespace CashFlow.Web.Areas.ShopManage.Service.Implement
{
    public class FeeService : MyService<FeeEntity>, IFeeService
    {
        public IList<FeeUserInfoModel> QueryByWhere(
            int shopManagerUserId
            , string date
            , string name)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var query = db.Queryable<FeeEntity>()
                              .InnerJoin<UserInfoEntity>((f, u) => f.UserId == u.Id)
                              .InnerJoin<ShopUserEntity>((f, u, su) => f.UserId == su.UserId)
                              .Where((f, u, su) => su.ShopManagerUserId == shopManagerUserId
                                && f.Date == date);

                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where((f, u, su) => u.Name.Contains(name));
                }

                var ls = query.OrderByDescending((f, u, su) => f.Date)
                              .Select((f, u, su) => new FeeUserInfoModel
                              {
                                  Date = f.Date,
                                  Name = u.Name,
                                  OrderProfitFee = f.OrderProfitFee,
                                  ShopManagerManageFee = f.ShopManagerManageFee,
                                  BoundFee = f.BoundFee,
                                  ConsignmentSaleFee = f.ConsignmentSaleFee
                              })
                              .ToList();
                return ls;
            }
        }
    }
}